/* EIX - IPC system
 * Copyright (C) 2008-2011 Jorge Luis Zapata
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 * If not, see <http://www.gnu.org/licenses/>.
 */
#include "Eix.h"
#include "eix_private.h"
/*============================================================================*
 *                                  Local                                     *
 *============================================================================*/
/*============================================================================*
 *                                 Global                                     *
 *============================================================================*/
Eix_Client * eix_client_new(Eix_Transport *t, Eix_Server *srv)
{
	Eix_Client *thiz;

	thiz = calloc(1, sizeof(Eix_Client));
	thiz->t = t;
	thiz->server = srv;
	thiz->user_data = eina_hash_string_superfast_new(NULL);

	return thiz;
}
/*============================================================================*
 *                                   API                                      *
 *============================================================================*/
/**
 *
 */
EAPI Eix_Transport * eix_client_transport_get(Eix_Client *thiz)
{
	return thiz->t;
}
/**
 *
 */
EAPI void eix_client_data_set(Eix_Client *thiz, const char *str, void *data)
{
	if (!thiz) return;
	eina_hash_add(thiz->user_data, str, data);
}

/**
 *
 */
EAPI void * eix_client_data_get(Eix_Client *thiz, const char *str)
{
	if (!thiz) return NULL;
	return eina_hash_find(thiz->user_data, str);
}

/**
 *
 */
EAPI void eix_client_del(Eix_Client *thiz)
{
	if (!thiz) return;

	eix_server_client_remove(thiz->server, thiz);
	eix_transport_free(thiz->t);
	free(thiz);
}

EAPI Eina_Bool eix_client_message_send(Eix_Client *thiz, Eix_Message *m,
		void *data, void *rdata, Eix_Error *err, uint32_t *id)
{
	if (!thiz) return EINA_FALSE;
	if (!thiz->t) return EINA_FALSE;

	return eix_transport_send(thiz->t, m, 0, data, rdata, err, id, NULL, NULL);
}

EAPI Eina_Bool eix_client_message_send_deferred(Eix_Client *thiz,
		Eix_Message *m, void *data, Eix_Transport_Reply_Cb cb,
		const void *user_data)
{
	if (!thiz) return EINA_FALSE;
	if (!thiz->t) return EINA_FALSE;

	return eix_transport_send(thiz->t, m, 0, data, NULL, NULL, NULL, cb, user_data);
}


/**
 *
 */
EAPI Eix_Server * eix_client_server_get(Eix_Client *c)
{
	return c->server;
}

EAPI void eix_client_process(Eix_Client *thiz, Eina_Bool can_read, Eina_Bool can_write)
{
       	eix_transport_process(thiz->t, can_read, can_write);
	if (thiz->delete_me)
		eix_client_del(thiz);
}
